package com.wswy.wzcx.utils

import android.app.Activity
import android.support.annotation.ColorRes
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.text.InputFilter
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.che.libcommon.utils.JsonKit
import com.google.gson.reflect.TypeToken
import com.wswy.wzcx.AppContext
import java.lang.reflect.Type

public fun <T> MutableIterator<T>.filterNotSelf(predicate: (T) -> Boolean) {
    val iterator = iterator()
    while (iterator.hasNext()){
        val next = iterator.next()
        if(!predicate(next)){
            remove()
        }
    }
    //return this
}

public inline fun <reified T:Any>  gsonTypeRef(): TypeToken<T> = object: TypeToken<T>() {}


public inline fun <reified T:Any>  gsonType(): Type = gsonTypeRef<T>().type

public inline fun <reified T:Any>  fromJson(json:String): T? {

    return JsonKit.GSON.fromJson(json, gsonType<T>())
}


public inline fun <T>  toJson(any: T): String? {
    return JsonKit.GSON.toJson(any)
}

public inline fun EditText.fillText(text:CharSequence?,lastSelection:Boolean=true){
    (this as TextView).text = text

    if (lastSelection){
        this.text?.let {
            setSelection(it.length)

        }
    }

}

public inline fun Int.append0():String{
    return if (this >= 10){
        toString()
    }else{
        "0$this"
    }
}

fun Activity.getEColor(@ColorRes resColor:Int):Int{
    return ContextCompat.getColor(applicationContext?:AppContext.getContext(),resColor)
}

fun Fragment.getColor(@ColorRes resColor:Int):Int{
    return ContextCompat.getColor(context?: AppContext.getContext(),resColor)
}

fun View.getColor(@ColorRes resColor:Int):Int{
    return ContextCompat.getColor(context?:AppContext.getContext(),resColor)
}